module LogiLang.Environment (
	Identifier,
	Pattern (..),
	Frame,
	Environment,
	lookupId,
	extend,
	insertVar
	) where

import qualified Data.Map as Map

type Identifier = String

data Pattern =
	Singleton String
	| Combination [Pattern]
	| Variable Identifier
	| DottedTail Identifier
	deriving (Eq, Show)

type Frame = Map.Map Identifier Pattern

type Environment = [Frame]

lookupId :: Environment -> Identifier -> Maybe Pattern
lookupId [] _ = Nothing
lookupId (hd:tl) ident =
	case Map.lookup ident hd of
		Nothing -> lookupId tl ident
		Just x -> Just x

extend :: Environment -> Map.Map Identifier Pattern -> Environment
extend env top = top : env

insertVar :: Environment -> Identifier -> Pattern -> Environment
insertVar [] var val = [Map.fromList [(var, val)]]
insertVar (hd:tl) var val = (Map.insert var val hd) : tl

